import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

Vue.use(Vuex)

let actions = {
	// 奇数加
	jiaOdd(miniStore,value){
		if(miniStore.state.sum % 2){
			miniStore.commit('JIA',value)
		}
	},
	//等一等加
	jiaDyd(miniStore,value){
		setTimeout(()=>{
			miniStore.commit('JIA',value)
		},500)
	},
	//联系服务器再加
	jiaFuwu(miniStore){
		axios.get('https://api.uixsj.cn/hitokoto/get?type=social').then(
			response => {
				alert(response.data)
				miniStore.commit('JIA',response.data.length)
			},
			error => {
				alert(error)
			}
		)
	}
}

//mutations是对数据进行最纯粹的操作，不允许：开定时器、发ajax请求等等
let mutations = {
	JIA(state,value){
		state.sum += value
	},
	JIAN(state,value){
		state.sum -= value
	}
}

let state = {
	sum:1, //和
	description:'这是atguigu的求和案例'
}

let getters = {
	bigSum(state){
		return state.sum * 10
	},
	bigDescription(state){
		return state.description.toUpperCase()
	}
}

//创建并暴露一个store对象
export default new Vuex.Store({
	actions,
	mutations,
	state,
	getters
})
